<?php

namespace app\api\controller;

use app\common\controller\Api;

use think\Validate;

/**
 * todo专项处理
 */
class Xcxtodo extends Api
{
    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];
    protected $uid = null;
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();

        $this->uid = $this->auth->id;
        $this->model = model('admin/Todo');
    }
    
    // 增加和修改
    public function addEdit($id=null)
    {
      $data = $this->request->post();
      $type = "新增";
      if(empty($id)){
        // 新增
        $data["userid"] = $this->uid;
        $res = $this->model
        ->allowField(true)
        ->save($data);
      }
      else{
        // 修改
        if(isset($data["userid"])){
          unset($data["userid"]);
        }
        $type = "修改";

        // $data["updatetime"] = time();
        // $res = $this->model
        // ->allowField(true)  
        // ->where(["id"=>$id,"userid"=>$this->uid])     
        // ->update($data);

        if($info = $this->model->get(['id'=>$id,'userid'=>$this->uid])){
            // 如果是修改已完成状态，则同时写入完成时间
            if(isset($data["status"])){
              if($data["status"] == 2){
                $data["donetime"] = time();
              }
              else if($data["status"] == 1){
                $data["donetime"] = null;
              }
            }
            $res = $info
            ->allowField(true)
            ->isUpdate(true)
            ->save($data);
        }
        else{
            $this->error($type."失败，未找到数据");
        }
      

      
      }

      if($res){
        $this->success($type."成功");
      }
      else{
        $this->error($type."失败");

      }
      
    }

    // 软删除数据
    public function delete($id)
    {
      $type = "删除".$id."数据";
      // $res = $this->model->get($id)->delete(); 
      if($info = $this->model->get(['id'=>$id,'userid'=>$this->uid])){
        $res = $info->delete(); 
      }
      else{
        $this->error($type."失败，未找到数据");
      }
      
      if($res){
          $this->success($type."成功");
      }
      else{
          $this->error($type."失败");
      }

    }
    
    // 查询数据
    public function list()
    {
      $task = input("task");
      $where=[
        "userid"=>$this->uid,
      ];
      if($task){
          $where["task"]=["like","%$task%"];
      }
      $list = $this->model->where($where)->select();
      
      $tags = model('admin/Todotags')->where([
        "userid"=>$this->uid,
      ])->column('tags','id');

      foreach ($list as $key => &$value) {
        $tagsArr = explode(',',$value["tags"]);

        $tagsNameArr = [];
        foreach ($tagsArr as $id) {
          $tagsNameArr[] = $tags[$id] ?? $id;
        }

        $value["tagsName"] = implode(',',$tagsNameArr);
      }
      
      
      $this->success("成功",$list );
    }

    // 获取标签列表
    public function tags()
    {
      $tags = input("tags");
      $where=[
          "userid"=>$this->uid,
      ];
      if($tags){
          $where["tags"]=["like","%$tags%"];
      }
      $list = model('admin/Todotags')->where($where)->select();
      $this->success("成功",$list);

    }

    // 根据标签查询任务列表
    public function getlistbytags()
    {
      $tags = input("tags");
      $where=[
          "userid"=>$this->uid,
      ];
      
      $list = $this->model
      ->where($where);

      if($tags){
          $list=$list->where('','exp', "instr(CONCAT( ',', tags, ',' ),  ',".$tags.",' )");
      }
      
      $list = $list->select();
      $this->success("成功",$list);
    }

    // 根据日期分组查询
    public function getbyday()
    {
      $where=[
        "userid"=>$this->uid,
        "status"=>1,
      ];
      // 已过期的
      $list1 =$this->model->where($where)->whereTime('todotime','<',time())->order('createtime desc')->select();
      // 今天的
      $list2 =$this->model->where($where)->whereTime('todotime','d')->whereTime('todotime','>=',time())->order('createtime desc')->select();

      // 明天的
      $day = date('Y-m-d',strtotime("+1 day"));
      $list3 =$this->model->where($where)->whereTime('todotime', 'between', [$day.' 00:00:00', $day.' 23:59:59'])->order('createtime desc')->select();

      // 2--7天内
      $day1 = date('Y-m-d',strtotime("+2 day"));
      $day2 = date('Y-m-d',strtotime("+7 day"));
      $list4 =$this->model->where($where)->whereTime('todotime', 'between', [$day1.' 00:00:00', $day2.' 23:59:59'])->order('createtime desc')->select();

      // 更久以后
      
      $list5 =$this->model->where($where)->whereTime('todotime', '>', $day2.' 23:59:59')->order('createtime desc')->select();

      // 无计划日期的
     
      $list6 =$this->model->where($where)->whereNull('todotime')->order('createtime desc')->select();

      // 已完成的
     
      $where["status"]=2;
      $list7 =$this->model->where($where)->select();

      
      $this->success("成功",[
        "timeout"=>$list1,
        "today"=>$list2,
        "tomorrow"=>$list3,
        "seven"=>$list4,
        "langtime"=>$list5,
        "notime"=>$list6,
        "timeend"=>$list7,
      ]);



    }

    // 已放弃的
    public function giveup()
    {
      $where=[
        "userid"=>$this->uid,
        "status"=>3,
      ];
      $list =$this->model->where($where)->select();
      $this->success("成功",$list);
    }

}
